package com.example.teach.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teach.entity.Result;
import com.example.teach.service.ClassService;
import com.example.teach.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.teach.entity.Clazz;


@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @Autowired
    private ScheduleService scheduleService;


    @PostMapping("/create")
    public Result create(@RequestParam("teacherId") Integer teacherId,
                         @RequestParam("className") String className,
                         @RequestParam("majorId") Integer majorId,
                         @RequestParam("courseId") Integer courseId,
                         @RequestParam("number") Integer number ){
        if(teacherId == null || className == null || courseId == null || number == null){
            return Result.error("参数不可为空");
        }
        // 先检查班级是否存在
        /*Clazz existingClazz = classService.selectByClassName(className);
        if(existingClazz != null && existingClazz.getState() == 0){
            return Result.error("班级名已存在");
        }*/
        classService.create(teacherId,className,majorId,courseId,number);
        return Result.success();
    }

    @PostMapping("/join")
    public Result join(@RequestParam("userId") Integer userId,
                       @RequestParam("classId") Integer classId){

        if(classService.selectByUserId(userId,classId) != null){
            return Result.error("学生已在班级中");
        }
        if(classService.selectClassNumber(classId) == 0){
            return Result.error("班级人数已满");
        }
        classService.join(userId,classId);
        return Result.success();

    }

    @DeleteMapping("/quit")
    public Result quitById(@RequestParam("userId") Integer userId,
                           @RequestParam("classId") Integer classId){
        if(classService.selectByUserId(userId,classId) == null){
            return Result.error("学生不在班级中");
        }
        classService.quit(userId,classId);
        return Result.success();
    }

    /**
     * 将state变成 1 同时删除排课信息
     * @param classId
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("classId") Integer classId){
        classService.deleteClassAndSchedule(classId);
        return Result.success();
    }

    @GetMapping("/pageAll")
    public Result pageAll(@RequestParam(value="pageNum",defaultValue="1") Integer pageNum,
                          @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize,
                          @RequestParam(value="teacherId") Integer teacherId){
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Clazz::getTeacherId,teacherId);
        queryWrapper.eq(Clazz::getState,0);
        return Result.success(classService.page(new Page<>(pageNum,pageSize),queryWrapper));

    }


    /**
     * 作废
     * @param classId
     * @param courseId
     * @return
     */
    @PostMapping("/addCourse")
    public Result addCourse(@RequestParam("classId") Integer classId,
                            @RequestParam("courseId") Integer courseId){
        if(classId == null || courseId == null){
            return Result.error("不能为空");
        }
        classService.setCourseId(classId,courseId);
        return Result.success();
    }


}
